#encoding: utf-8
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_unicode

from usercenter.models import Users,Department
# Create your models here.


WENJUAN_STATUS = (
    (0, u"发布"),
    (1, u"关闭"),
)

TIMU_LEIXING = (
    (0, u"单选题"),
    (1, u"多选题"),
    (2, u"判断题"),
    (3, u"填空题"),
)



class wenjuan(models.Model):
	"""
	问卷
	"""

	name=models.CharField(u'问卷名称',max_length=128)
	breif=models.TextField(u'问卷简介')
	danwei=models.ForeignKey(Department, verbose_name=u'所属单位')
	status = models.SmallIntegerField(choices=WENJUAN_STATUS, verbose_name=u'状态')
	fabu_time = models.DateTimeField(verbose_name=u'发布日期',null=True,blank=True)
	jiezhi_time = models.DateTimeField(verbose_name=u'发布日期',null=True,blank=True)
	create_time = models.DateTimeField(auto_now=True, verbose_name=u'创建日期',null=True,blank=True)
	cjUser=models.ForeignKey(Users,related_name='users_cj',  verbose_name=u'所属用户',null=True,blank=True)

	def __unicode__(self):
		return self.name

	class Meta:
		verbose_name = u"问卷"
		verbose_name_plural = verbose_name
		#app_label = _(u'b')

class timu(models.Model):
	"""
	题目
	"""
	tigan=models.TextField(u'题干')
	shunxu=models.IntegerField(verbose_name='显示顺序',default=0)
	leixing = models.SmallIntegerField(choices=TIMU_LEIXING,default=0, verbose_name=u'试题类型')
	wenjuan=models.ForeignKey(wenjuan,related_name='wenjuan_timu',verbose_name=u'所属问卷')
	xuanxiang01=models.CharField(u'选项A',max_length=128,null=True,blank=True)
	xuanxiang02=models.CharField(u'选项B',max_length=128,null=True,blank=True)
	xuanxiang03=models.CharField(u'选项C',max_length=128,null=True,blank=True)
	xuanxiang04=models.CharField(u'选项D',max_length=128,null=True,blank=True)
	xuanxiang05=models.CharField(u'选项E',max_length=128,null=True,blank=True)
	xuanxiang06=models.CharField(u'选项F',max_length=128,null=True,blank=True)
	xuanxiang07=models.CharField(u'选项G',max_length=128,null=True,blank=True)
	xuanxiang08=models.CharField(u'选项H',max_length=128,null=True,blank=True)

	def getControlName(self):
		name="%d_%d_%d"%(self.wenjuan.id,self.shunxu,self.id)
		return name

	def getxuanxiang(self):
		xuanstr=''
		name=self.getControlName()
		value=0
		if self.xuanxiang01 != '':
			vstr=self.xuanxiang01
			value=value+1
			xuanstr+=self.danxuanHTML(name,vstr,value)
		if self.xuanxiang02 != '':
			vstr=self.xuanxiang02
			value=value+1
			xuanstr+=self.danxuanHTML(name,vstr,value)

		if self.xuanxiang03 != '':
			vstr=self.xuanxiang03
			value=value+1
			xuanstr+=self.danxuanHTML(name,vstr,value)

		if self.xuanxiang04 != '':
			vstr=self.xuanxiang04
			value=value+1
			xuanstr+=self.danxuanHTML(name,vstr,value)

		if self.xuanxiang05 != '':
			vstr=self.xuanxiang05
			value=value+1
			xuanstr+=self.danxuanHTML(name,vstr,value)

		if self.xuanxiang06 != '':
			vstr=self.xuanxiang06
			value=value+1
			xuanstr+=self.danxuanHTML(name,vstr,value)

		if self.xuanxiang07 != '':
			vstr=self.xuanxiang07
			value=value+1
			xuanstr+=self.danxuanHTML(name,vstr,value)
		
		if self.xuanxiang08 != '':
			vstr=self.xuanxiang08
			value=value+1
			xuanstr+=self.danxuanHTML(name,vstr,value)

		return xuanstr



	def danxuanHTML(self,id,vstr,value):
		return '<label class="radio"><input id='+str(self.shunxu)+' type="radio" name="'+str(id)+'" value="'+str(value)+'">'+vstr+'</input></label>\n'




	def __unicode__(self):
		return self.tigan


	class Meta:
		verbose_name = u"题目"
		verbose_name_plural = verbose_name
		#app_label = _(u'b')


class yuanshiJieguo(models.Model):
	wjkey=models.CharField(u'结果唯一标识',max_length=128)
	username=models.CharField(u'用户名',max_length=128,null=True,blank=True)
	ipaddress=models.CharField(u'ip地址',max_length=20)
	wenjuanItem=models.ForeignKey(wenjuan,related_name='wenjuan_jieguo_yuanshi',verbose_name=u'所属问卷')
	value =models.CharField(u'结果值',max_length=128)
	create_time = models.DateTimeField(auto_now=True, verbose_name=u'创建日期',null=True,blank=True)

	def __unicode__(self):
		return self.ipaddress


	class Meta:
		verbose_name = u"原始结果"
		verbose_name_plural = verbose_name


class jieguo(models.Model):
	username=models.CharField(u'用户名',max_length=128,null=True,blank=True)
	ipaddress=models.CharField(u'ip地址',max_length=20)
	jgtimu=models.ForeignKey(timu,related_name='jgtimu')
	wenjuan=models.ForeignKey(wenjuan,related_name='wenjuan_jieguo',verbose_name=u'所属问卷')
	value =models.CharField(u'结果值',max_length=128)
	create_time = models.DateTimeField(auto_now=True, verbose_name=u'创建日期',null=True,blank=True)

	def __unicode__(self):
		return self.ipaddress


	class Meta:
		verbose_name = u"结果"
		verbose_name_plural = verbose_name


class chafenJieguo(models.Model):
	wjkey=models.CharField(u'结果唯一标识',max_length=128)
	username=models.CharField(u'用户名',max_length=128,null=True,blank=True)
	ipaddress=models.CharField(u'ip地址',max_length=20)
	wenjuanItemID=models.IntegerField(verbose_name=u'所属问卷ID')
	timuItemID=models.IntegerField(verbose_name=u'所属题目ID')
	shunxuID=models.IntegerField(verbose_name=u'题号')
	value =models.IntegerField(verbose_name=u'结果')
	create_time = models.DateTimeField(auto_now=True, verbose_name=u'创建日期',null=True,blank=True)

	def __unicode__(self):
		return self.wjkey


	class Meta:
		verbose_name = u"拆分结果"
		verbose_name_plural = verbose_name






